<?php
class Zend_Template_DbModel{
    /**
     * @var 
     */
    private $_table;
    
    /**
    * Array query
    * 
    * @var array
    */
    private $_forms;
    
    /**
     * Construct
     * @author tien.nguyen
     */
    public function __construct($table, $forms){
        $this->_table = $table;
        $this->_forms = $forms;
    }
    
    /**
     * render 
     * @author tien.nguyen
     */
    public function render(){
        global $smarty;
        $smarty->assign('table', $this->_table);
        $smarty->assign('forms', $this->_forms);
        $smarty->assign('path_template', PATH_STRUCTURE.'/zend-framework/templates/models/db');
        return $smarty->fetch(PATH_STRUCTURE.'/zend-framework/templates/models/dbmodel.tpl');
    }
    
    /**
     * Write file
     * @author tien.nguyen
     */
    public function writeFile(){
        $template = $this->render();
        
        $pathDbTable = PATH_PROJECT_GENERAL.'/'.
            NAME_PROJECT_GENERAL.'/application/models/DbTable';
        
        //create folder if !exist
        if(!file_exists($pathDbTable)){
                mkdir($pathDbTable, 777);
        }
        
        //path db table
        $path = $pathDbTable.'/'. $this->_table->table_class.'.php';
        
        $hand = fopen($path, 'w');
        fwrite($hand, $template);
        fclose($hand);
    }
}
